package cn.itcast.web.sevlet;

import cn.itcast.domain.Student;
import cn.itcast.service.impl.BaseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//检测账号密码
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Student stu = new BaseServiceImpl().findByStudentnameAndPassword(username, password);
        if(stu!=null){
           request.getSession().setAttribute("username",stu.getName());
           //重定向
           response.sendRedirect(request.getContextPath()+"/successServlet");
        }else {//如果有数据,说明账号密码正确
            //请求转发
            request.getSession().setAttribute("error","账户密码不正确");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }
}
